

#include <cegame/ced3d9textureloader.h>
#include <d3dx9.h>


ceD3D9TextureLoader::ceD3D9TextureLoader ()
{
}

bool ceD3D9TextureLoader::CanLoad (iFile* file) const
{
	ceString ext = file->GetExtension ();
	if (	ext == "jpg" 
		||	ext == "jpeg"
		||	ext == "png"
		||	ext == "bmp"
		||  ext == "tga")
	{
		return true;
	}
	return false;
}


LPDIRECT3DTEXTURE9 ceD3D9TextureLoader::Load (LPDIRECT3DDEVICE9 device, iFile* file)
{
	file->Seek (iFile::SP_End);
	size_t length = file->Tell ();
	file->Seek (iFile::SP_Set);

	char* buffer = new char[length];
	size_t read = file->Read (buffer, length);
	if (read != length)
	{
		delete []  buffer;
		return 0;
	}

	LPDIRECT3DTEXTURE9 txt = 0;
	if (FAILED(D3DXCreateTextureFromFileInMemory (
		device,
		buffer, 
		length,
		&txt)))
	{
		delete [] buffer;
		return 0;
	}
	delete [] buffer;
	return txt;
}

